#include "mainwindow.h"
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QCloseEvent>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QStackedLayout>
#include <QStatusBar>

#include "logwidget.h"
#include "setupparamdlg.h"
#include "sysstatuswidget.h"
#include "coviewdatareceiver.h"
#include "logger.h"
#include "mainboardreceiver.h"
#include "myiconlabelwidget.h"

#include "dataitemdef.h"
MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint )
{
    setFixedSize( QSize(MAIN_WINDOW_WIDTH,MAIN_WINDOW_HEIGHT) );
    m_pMainLayout = new QVBoxLayout( this );

    CreateActions();
    CreateToolbarActions();
    CreateRenderWidgets();
    CreateStatusBar();    

    CreateCloseButton();
    CreateLogWidget();

    CreateCoViewDataRecevier();
    CreateMainBoardReceiver();

    AddLogMessage("启动软件");
}

MainWindow::~MainWindow()
{
}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::CreateActions
 */
void MainWindow::CreateActions()
{
    // 系统设置
    QIcon icon(":/images/icon_setup.png");
    m_pActionSetup = new QAction( icon, "设置", this );
    connect( m_pActionSetup, &QAction::triggered, this, &MainWindow::slot_SystemSetup );

    // 运行日志
    icon = QIcon(":/images/icon_log.png");
    m_pActionShowLog = new QAction( icon, "日志", this );
    connect( m_pActionShowLog, &QAction::triggered, this, &MainWindow::slot_ShowRuntimeLog );

    CreateMeasureActions();
}

//--------------------------------------------------
// CYJ, 2022.01.17
/**
 * @brief MainWindow::CreateMeasureActions
 */
void MainWindow::CreateMeasureActions()
{
    // 10MHz
    QIcon icon(":/images/N_10MHz.png");
    m_pActionMeasure_10MHz = new QAction( icon, "10MHz", this );
    connect( m_pActionMeasure_10MHz, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量10MHz...");
    });

    m_pActionMeasure_10MHz->setCheckable( true );

    // TTL
    icon = QIcon(":/images/N_TTL.png");
    m_pActionMeasure_TTL = new QAction( icon, "TTL", this );
    connect( m_pActionMeasure_TTL, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量TTL...");
    });

    m_pActionMeasure_TTL->setCheckable( true );

    // RS422
    icon = QIcon(":/images/N_RS422.png");
    m_pActionMeasure_RS422 = new QAction( icon, "RS422", this );
    connect( m_pActionMeasure_RS422, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量RS422...");
    });

    m_pActionMeasure_RS422->setCheckable( true );

    // ToD
    icon = QIcon(":/images/N_TOD.png");
    m_pActionMeasure_ToD = new QAction( icon, "ToD", this );
    connect(m_pActionMeasure_ToD,&QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量TOD...");
    });

    m_pActionMeasure_ToD->setCheckable( true );

    // BAC
    icon = QIcon(":/images/N_BAC.png");
    m_pActionMeasure_BAC = new QAction( icon, "BAC", this );
    connect( m_pActionMeasure_BAC, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量BAC...");
    });

    m_pActionMeasure_BAC->setCheckable( true );

    // NTP
    icon = QIcon(":/images/N_NTP.png");
    m_pActionMeasure_NTP = new QAction( icon, "NTP", this );
    connect( m_pActionMeasure_NTP, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量NTP...");
    });

    m_pActionMeasure_NTP->setCheckable( true );

    // PTP
    icon = QIcon(":/images/N_PTP.png");
    m_pActionMeasure_PTP = new QAction( icon, "PTP", this );
    connect( m_pActionMeasure_PTP, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量PTP...");
    });

    m_pActionMeasure_PTP->setCheckable( true );

    // 2048KHz
    icon = QIcon(":/images/N_2048.png");
    m_pActionMeasure_2048KHz = new QAction( icon, "2048KHz", this );
    connect( m_pActionMeasure_2048KHz, &QAction::triggered,[=](){
        QMessageBox::information(this,"提示","开始测量SIG...");
    });

    m_pActionMeasure_2048KHz->setCheckable( true );
}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::CreateToolbar
 */
void MainWindow::CreateToolbarActions()
{
    m_pToolbar = new QToolBar( this );
    m_pMainLayout->addWidget( m_pToolbar );

    m_pToolbar->setFloatable( false );
    m_pToolbar->setMovable( false );
    m_pToolbar->setAllowedAreas( Qt::TopToolBarArea );
    m_pToolbar->setIconSize( QSize( ICON_WIDTH*2, ICON_WIDTH ) );
    m_pToolbar->setMaximumWidth( MAIN_WINDOW_WIDTH - ICON_WIDTH - 4 );
    m_pToolbar->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );

    m_pToolbar->addAction( m_pActionSetup );
    m_pToolbar->addAction( m_pActionShowLog );

    m_pToolbar->addSeparator();
    m_pToolbar->addAction( m_pActionMeasure_10MHz );
    m_pToolbar->addAction( m_pActionMeasure_TTL );
    m_pToolbar->addAction( m_pActionMeasure_RS422 );
    m_pToolbar->addAction( m_pActionMeasure_ToD );
    m_pToolbar->addAction( m_pActionMeasure_BAC );
    m_pToolbar->addAction( m_pActionMeasure_NTP );
    m_pToolbar->addAction( m_pActionMeasure_PTP );
    m_pToolbar->addAction( m_pActionMeasure_2048KHz );
}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::CreateCloseButton
 */
void MainWindow::CreateCloseButton()
{
    QIcon icon(":/images/close.png");
    m_pBnClose = new QPushButton( icon, "", this );
    m_pBnClose->setIconSize( QSize( ICON_WIDTH, ICON_WIDTH ) );
    m_pBnClose->setFlat( true );
    m_pBnClose->resize( ICON_WIDTH, ICON_WIDTH );
    connect( m_pBnClose, &QPushButton::clicked, this, &QWidget::close );
}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::resizeEvent
 * @param event
 */
void MainWindow::resizeEvent( QResizeEvent * )
{
    QSize widgetSize = size();
    m_pBnClose->move( widgetSize.width() - ICON_WIDTH - 2, 2 );
    m_pBnClose->raise();

    m_pLogWidget->move( x() + MAIN_WINDOW_WIDTH/4, y() + 60 );    

}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::closeEvent
 * @param event
 */
void MainWindow::closeEvent( QCloseEvent * event )
{
    int nRetVal = QMessageBox::question( this, "确认", "确定退出？" );
    if( nRetVal != QMessageBox::Yes )
    {
        event->ignore();
        return;
    }
    event->accept();
}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::CreateStatusBar
 */
void MainWindow::CreateStatusBar()
{
    m_pStatusBar = new QStatusBar( this );
    m_pMainLayout->addWidget( m_pStatusBar );

    QIcon icon( ":/images/receiver_ok.png" );
    m_pGnnsReceiverOnlineStatus = new CMyIconLabelWidget( icon, "共视", this );
    m_pGnnsReceiverOnlineStatus->setToolTip( "共视接收机在线" );
    m_pStatusBar->addPermanentWidget( m_pGnnsReceiverOnlineStatus );

    icon = QIcon( ":/images/icon_board_ok.png" );
    m_pMainBoardOnlineStatus = new CMyIconLabelWidget( icon, "主板", this );
    m_pMainBoardOnlineStatus->setToolTip( "主板在线" );
    m_pStatusBar->addPermanentWidget( m_pMainBoardOnlineStatus );

    m_pStatusBar->showMessage( "hello" );
}

//--------------------------------------------------
// CYJ, 2022.01.07
/**
 * @brief MainWindow::CreateLogWidget
 */
void MainWindow::CreateLogWidget()
{
    m_pLogWidget = new CLogWidget( this );
    m_pLogWidget->resize( MAIN_WINDOW_WIDTH/2, MAIN_WINDOW_HEIGHT*3/4 );
    m_pLogWidget->move( x() + MAIN_WINDOW_WIDTH/4, y() + 20 );
    m_pLogWidget->show();
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::AddLogMessage
 * @param strMsg
 */
void MainWindow::AddLogMessage( QString strMsg )
{
    if( nullptr == m_pLogWidget )
        return;

    m_pLogWidget->addMessage( strMsg );
    m_pStatusBar->showMessage( strMsg );
}

//--------------------------------------------------
// CYJ, 2022.01.10
/**
 * @brief MainWindow::slot_SystemSetup
 */
void MainWindow::slot_SystemSetup()
{
    CSetupParamDlg dlg( this );

    if( m_pCoViewDataReceiver )
    {
        connect( &dlg, SIGNAL( signal_GNNSPortChanged() ),\
                 m_pCoViewDataReceiver, SLOT( slot_LocalPortChanged() ) );
    }
    if( m_pMainBoardReceiver )
    {
        connect( m_pMainBoardReceiver, SIGNAL( signal_CmdResponse(QString, QStringList) ),
                 &dlg, SLOT( slot_OnCmdResponse(QString, QStringList) ) );
    }

    dlg.exec();
}

//--------------------------------------------------
// CYJ, 2022.01.10
/**
 * @brief MainWindow::slot_ShowRuntimeLog
 */
void MainWindow::slot_ShowRuntimeLog()
{
    if( m_pLogWidget->isVisible() )
    {
        m_pLogWidget->hide();
        return;
    }

    m_pLogWidget->move( x() + MAIN_WINDOW_WIDTH/4, y() + 60 );
    m_pLogWidget->setVisible( true );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::CreateRenderWidgets
 */
void MainWindow::CreateRenderWidgets()
{
    m_pRenderWidgetLayout = new QStackedLayout;
    m_pMainLayout->addLayout( m_pRenderWidgetLayout );

    m_pSysStatusWidget = new CSysStatusWidget( this );
    m_pRenderWidgetLayout->addWidget( m_pSysStatusWidget );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_10MHz
 */
void MainWindow::slot_Measure_10MHz(TMB_10MHz m_last10MHz)
{
    my_dbg_printf( "[10MHz] 1ms=%.12lf, 10ms=%.12lf, 100ms=%.12lf, 1s=%.12lf, cxp=%d, freqpw=%.12lf, fz=%d\n",\
                   m_last10MHz.m_f1MS,\
                   m_last10MHz.m_f10MS,\
                   m_last10MHz.m_f100MS,\
                   m_last10MHz.m_f1S,\
                   m_last10MHz.m_nCXP,\
                   m_last10MHz.m_fFreqPW,\
                   m_last10MHz.m_nFZ );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_TTL
 */
void MainWindow::slot_Measure_TTL(TMB_SIGTIE m_lastSigTie)
{
    my_dbg_printf( "[TTLTIE] 2048KHz=%lld, TTL=%lld, RS422=%lld\n",\
                   m_lastSigTie.m_ll2048KHz,\
                   m_lastSigTie.m_llTTL,\
                   m_lastSigTie.m_llRS422 );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_RS422
 */
void MainWindow::slot_Measure_RS422(TMB_TOD_BDC_BAC outData)
{
    my_dbg_printf( "[RS422] date=%08d, time=%06d, value=%lld\n",\
                   outData.m_dwDate, outData.m_dwTime, outData.m_llValue  );

}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_ToD
 */
void MainWindow::slot_Measure_ToD(TMB_TOD_BDC_BAC outData)
{
    my_dbg_printf( "[TODTIE] date=%08d, time=%06d, value=%lld\n",\
                   outData.m_dwDate, outData.m_dwTime, \
                   outData.m_llValue );

}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_BAC
 */
void MainWindow::slot_Measure_BAC(TMB_TOD_BDC_BAC outData)
{
    my_dbg_printf( "[BACTIE] date=%08d, time=%06d, value=%lld\n",\
                   outData.m_dwDate, outData.m_dwTime, \
                   outData.m_llValue );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_NTP
 */
void MainWindow::slot_Measure_NTP(TMB_NTP_PTP outData)
{
    my_dbg_printf( "[NTPTIE] date=%08d, time=%06d, offset=%lld, delay=%lld\n",\
                   outData.m_dwDate, outData.m_dwTime, \
                   outData.m_llOffset, outData.m_llDelay );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_PTP
 */
void MainWindow::slot_Measure_PTP(TMB_NTP_PTP outData)
{
    my_dbg_printf( "[PTPTIE] date=%08d, time=%06d, offset=%lld, delay=%lld\n",\
                   outData.m_dwDate, outData.m_dwTime, \
                   outData.m_llOffset, outData.m_llDelay );
}

//--------------------------------------------------
// CYJ, 2022.01.16
/**
 * @brief MainWindow::slot_Measure_2048KHz
 */
void MainWindow::slot_Measure_2048KHz(TMB_SIGTIE m_lastSigTie)
{
    my_dbg_printf( "[SIGTIE] 2048KHz=%lld, TTL=%lld, RS422=%lld\n",\
                   m_lastSigTie.m_ll2048KHz,\
                   m_lastSigTie.m_llTTL,\
                   m_lastSigTie.m_llRS422 );
}

//--------------------------------------------------
// CYJ, 2022.02.03
/**
 * @brief 创建共视数据接收
 */
void MainWindow::CreateCoViewDataRecevier()
{
    m_pCoViewDataReceiver = new CCoviewDataReceiver( this );
    if( nullptr == m_pCoViewDataReceiver )
    {
        my_dbg_printf( "new CCoviewDataReceiver failed.\n" );
        return;
    }

    connect( m_pCoViewDataReceiver, SIGNAL( signal_OnCoViewData(const COVIEW_DATAITEM&) ),\
             this, SLOT( slot_OnCoViewData(const COVIEW_DATAITEM&) ) );

    int nRetVal = m_pCoViewDataReceiver->Initialize();
    if( nRetVal )
    {
        my_dbg_printf( "Initialize CCoviewDataReceiver() = %d.\n", nRetVal );
        delete m_pCoViewDataReceiver;
        m_pCoViewDataReceiver = nullptr;
        return;
    }
}

//--------------------------------------------------
// CYJ, 2022.02.03
/**
 * @brief MainWindow::slot_OnCoViewData
 * @param CoViewDataItem
 */
void MainWindow::slot_OnCoViewData( const COVIEW_DATAITEM& CoViewDataItem )
{
    m_pSysStatusWidget->updateCoViewData( CoViewDataItem );
}

//--------------------------------------------------
// CYJ, 2022.02.13
/**
 * @brief MainWindow::CreateMainBoardReceiver
 */
void MainWindow::CreateMainBoardReceiver()
{
    m_pMainBoardReceiver = new CMainBoardReceiver( this );
    if( nullptr == m_pMainBoardReceiver )
    {
        my_dbg_printf( "new CMainBoardReceiver failed.\n" );
        return;
    }

    connect( m_pMainBoardReceiver, SIGNAL( signal_GetDataFromMainboard() ),
             this, SLOT( slot_GetDataFromMainboard() ) );
    connect( m_pMainBoardReceiver, SIGNAL( signal_SysInfoData(const TMB_SYSINFO& ) ),
             this, SLOT( slot_SysInfoData(const TMB_SYSINFO&) ) );
    connect( m_pMainBoardReceiver, SIGNAL( signal_IOSBV(const TMB_IOSBV& ) ),
             this, SLOT( slot_GetIOSBV(const TMB_IOSBV&) ) );


    int nRetVal = m_pMainBoardReceiver->Initialize();
    if( nRetVal )
    {
        my_dbg_printf( "Initialize CMainBoardReceiver() = %d.\n", nRetVal );
        delete m_pMainBoardReceiver;
        m_pMainBoardReceiver = nullptr;
        return;
    }
}

//--------------------------------------------------
// CYJ, 2022.02.13
/**
 * @brief MainWindow::slot_GetDataFromMainboard
 */
void MainWindow::slot_GetDataFromMainboard()
{

}

//--------------------------------------------------
// CYJ, 2022.02.13
/**
 * @brief slot_SysInfoData
 * @param SysInfo               系统状态数据
 */
void MainWindow::slot_SysInfoData( const TMB_SYSINFO& SysInfo )
{
    m_pSysStatusWidget->UpdateSysInfoData( SysInfo );
}

//--------------------------------------------------
// CYJ, 2022.02.13
/**
 * @brief slot_SysInfoData
 * @param IoSBV                 观测数据
 */
void MainWindow::slot_GetIOSBV( const TMB_IOSBV& IoSBV )
{
    m_pSysStatusWidget->UpdateIOSBVData( IoSBV );
}
